﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using TA.DotNetUtils;
using System.Collections;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Renders the contents of the Repeater in server-side binding scenarios.
	/// </summary>
	public class RepeaterRenderedItemsContainer : BaseRepeaterControl
	{
		#region PUBLIC
		#region INIT
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		/// <summary>
		/// Renders the contents.
		/// </summary>
		/// <param name="writer"></param>
		public override void RenderControl(HtmlTextWriter writer)
		{
			if (this.Repeater.EnableServerRender)
			{
				MultipleInstanceTemplate t = this.Repeater.DisplayEdit ? this.Repeater.EditItem : this.Repeater.ViewItem;

				this.DoRender(writer, t);
			}
			base.RenderControl(writer);
		}
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		internal RepeaterRenderedItemsContainer(Repeater owner)
			: base(owner)
		{
		}
		#endregion
		#region PROPS
		protected IRepeaterEntries Entries
		{
			get
			{
				return this.Repeater.Value.Entries;
			}
		}
		#endregion
		#region METHODS
		protected virtual void DoRender(HtmlTextWriter writer, MultipleInstanceTemplate t)
		{
			IntRange entriesRange = this.Repeater.EnsureEntriesLoaded(this.Repeater.EntriesRange);

			for (int i = entriesRange.Start; i < entriesRange.End; ++i)
			{
				this.Repeater.BindDataToItem(t, this.Repeater.Value.Entries, i - this.Repeater.Value.FirstIndex);
				t.Render(writer, (i - entriesRange.Start).ToString());
			}
		}
		#endregion
		#endregion				
	}
}
